<template>
  <div class="app-container">
    <el-button v-if="checkPermission(['admin:menu:saveOrUpdate'])" type="primary" @click="addMenu">增加</el-button>
    <el-table
      :data="list"
      style="width: 100%;margin-top: 10px;"
      row-key="id"
      border
      lazy
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="menuTitle"
        label="路由标题">
      </el-table-column>
      <el-table-column
        prop="menuPath"
        label="路由路径"
        align="center">
      </el-table-column>
      <el-table-column
        prop="menuName"
        label="路由名称"
        align="center">
      </el-table-column>
      <el-table-column
        prop="menuIcon"
        label="图标"
        align="center">
        <template slot-scope="scope">
          <i :class="scope.row.menuIcon"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="menuType"
        label="类型"
        align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.menuType === 0">目录</el-tag>
          <el-tag type="success" v-if="scope.row.menuType === 1">菜单</el-tag>
          <el-tag type="info" v-if="scope.row.menuType === 2">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="menuSort"
        label="排序号"
        align="center">
      </el-table-column>
      <el-table-column
        prop="menuUrl"
        label="菜单URL"
        align="center">
      </el-table-column>
      <el-table-column
        prop="menuPers"
        label="授权标识"
        align="center">
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        width="100"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(['admin:menu:info'])" type="text" size="small" @click="editHandle(scope.row.id)">修改</el-button>
          <el-button v-if="checkPermission(['admin:menu:remove'])" type="text" size="small" @click="removeHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <menu-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></menu-add-or-update>
  </div>
</template>

<script>
  import MenuAddOrUpdate from './menu-add-or-update.vue'
  import adminMenuApi from '@/api/core/sys/admin-menu'
  export default {
    data () {
      return {
        list: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      MenuAddOrUpdate
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 查询
      getDataList() {
        adminMenuApi.listAll().then(res => {
          this.list = res.data.menus
        }).catch(res => {
          this.ligt = []
        })
      },
      // 修改
      editHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      removeHandle(id) {
        this.$confirm(`确定对[id=${id}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          adminMenuApi.removeById(id).then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(res.message)
            }
          }).catch()
        }).catch(() => {
          this.$message('取消')
        })
      },
      // 增加
      addMenu() {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init()
        })
      }
    }
  }
</script>

<style scoped></style>